package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.RRException;
import org.springframework.web.bind.annotation.RequestParam;
import yf.mall.entity.*;
import com.yf.response.R;
import yf.mall.enums.MallActStatusEnum;
import yf.mall.enums.MallGoodsShelfEnum;
import yf.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RequestMapping("/api/goods")
@RestController
public class ApiGoodsController {

    @Autowired
    private MallGoodsService mallGoodsService;

    @Autowired
    private MallTypeService mallTypeService;

    @Autowired
    private MallActService mallActService;


    @GetMapping("/list")
    public R list(@LoginUserId Long userId, int page, int limit, @RequestParam(required = false) Integer type, String order) {
        if (order == null) {
            order = "sort,id Asc";
        }
        //Sort正排序  ID反排序
        PageHelper.startPage(page, limit).setOrderBy(order);
        Map map = new HashMap();

        map.put("shelf", MallGoodsShelfEnum.ON.getIndex());//已上架
        List<MallGoodsEntity> mallGoodsEntities;
        if (type == null || type == 0){
            mallGoodsEntities = mallGoodsService.queryList(map);
        }else {
            //此操作是首页tab分类 一级分类没有数据时 显示所有二级分类下的商品
            mallGoodsEntities = homeType(type);
        }

        // 根据级别筛选
        mallGoodsEntities = mallGoodsService.filterByLevel(userId, mallGoodsEntities);
        //获取活动
        getAct(mallGoodsEntities);
        return R.ok(mallGoodsEntities);
    }

    /**
     * 查询所有分类
     *
     * @param params
     * @return
     */
    @RequestMapping("/type")
    @IgnoreAuth
    public R typeList(@RequestParam Map<String, Object> params) {
        PageHelper.orderBy("sort");
        List<MallTypeEntity> mallTypeEntities = mallTypeService.queryList(params);
        return R.ok(mallTypeEntities);
    }
    /**
     * 根据产品名查询
     *
     * @param
     * @return
     */
    @RequestMapping("/search")
    @IgnoreAuth
    public R search( String name,int page,int limit) {
        String order = "sort,id Asc";
        PageHelper.startPage(page, limit).setOrderBy(order);
        List<MallGoodsEntity> goodsEntityList = mallGoodsService.searchName(name);
        getAct(goodsEntityList);
        return R.ok(goodsEntityList);
    }

    //quequnlong 获取活动
    private void getAct(List<MallGoodsEntity> mallGoodsEntities) {
        for (MallGoodsEntity mallGoodsEntity : mallGoodsEntities) {
            MallActEntity mallActEntity = mallActService.findByGoodsIdAndstatus(mallGoodsEntity.getId(), MallActStatusEnum.OPEN.getIndex());
            if (null!= mallActEntity){
                mallGoodsEntity.setAct(mallActEntity);
            }
        }
    }

    // quequnlong  此操作是首页tab分类 一级分类没有数据时 显示所有二级分类下的商品
    public List<MallGoodsEntity> homeType(Integer type){

        Long typeId = type.longValue();
        List<MallGoodsEntity> list = new ArrayList<>();

        if (type != null){
            //获取该一级分类下的二级分类
            List<MallTypeEntity> parentType = mallTypeService.findByParentId(typeId);
            for (MallTypeEntity mallTypeEntity : parentType) {
                //根据分类获取产品id集合
                List<Long> goodsIds = mallTypeService.findByType(mallTypeEntity.getId());
                if (goodsIds != null && goodsIds.size() > 0){
                    for (Long goodId : goodsIds) {
                        System.out.println(goodId);
                        MallGoodsEntity mallGoodsEntity = mallGoodsService.get(goodId);
                        if (mallGoodsEntity.getShelf() != null && MallGoodsShelfEnum.ON.getIndex() == mallGoodsEntity.getShelf()){
                            list.add(mallGoodsEntity);
                        }
                    }
                }
            }
        }
        Collections.sort(list,(o1,o2) ->{
            if ( null!= o1.getSort() && null != o2.getSort()){
                if (o1.getSort() > o2.getSort()){
                    return 1;
                }
            }
            return -1;
        });
        return list;
    }
}
